package com.hangzhou.xyj.ui.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.engine.impl.AppUpdateEngineImpl;
import com.hangzhou.xyj.model.SingModel;
import com.hangzhou.xyj.model.SingPageInfoModel;
import com.hangzhou.xyj.model.type.EListType;
import com.hangzhou.xyj.net.handler.SingFriendHandler;
import com.hangzhou.xyj.net.handler.SingProfessHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.SingFriendReq;
import com.hangzhou.xyj.net.request.SingProfessReq;
import com.hangzhou.xyj.ui.adapter.TopListAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.DividerItemDecoration;

import java.util.List;

/**
 * Created by neo.duan on 2015/12/23.
 * 唱戏榜单
 */
public class TopListFragment extends BaseFragment {
    public static final int TYPE_PROFESSIONAL = 1;//专业
    public static final int TYPE_DRAMA_FRIENDS = 2;//戏友

    private int type;//是专业还是戏友
    private EListType mCurrentListType;//当前显示的榜单类型

    private RecyclerView mRecyclerView;
    private GridLayoutManager mGridLayoutManager;
    private TopListAdapter mAdapter;

    @Override
    public void initTop() {
        enableTop(false);
    }

    public EListType getCurrentListType() {
        return mCurrentListType;
    }

    public void setCurrentListType(EListType mCurrentListType) {
        this.mCurrentListType = mCurrentListType;
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.layout_professional_list_fragment);
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_professional_list);

        // 设置水平布局
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mGridLayoutManager = new GridLayoutManager(mContext, 3));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置竖直分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST
                , R.drawable.line_divider));
        //设置水平分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL_LIST
                , R.drawable.line_divider));
        mRecyclerView.setAdapter(mAdapter = new TopListAdapter(mContext, null));

    }


    @Override
    public void initDatas() {
        //设置item位置摆放
        mGridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {
                switch (position) {
                    case 0:
                    case 1:
                        return mGridLayoutManager.getSpanCount();
                    default:
                        return 1;
                }
            }
        });
        mRecyclerView.setLayoutManager(mGridLayoutManager);
        //初始化默认请求的是 总榜
        type = getArguments().getInt("type");
        if (type == TYPE_PROFESSIONAL) {
            sendAndGetProfessListData(50, 1, EListType.MAIN);
        } else {
            sendAndGetDramaListData(50, 1, EListType.MAIN);
        }
    }

    @Override
    public void initListeners() {

    }


    /**
     * 请求服务器，并获取 榜单-戏友 数据
     *
     * @param listType 榜单类型
     * @return
     * @author yaohuix
     * @date 2016/3/10 17:11
     */
    public void sendAndGetDramaListData(int pageSize, int currentPage, EListType listType) {
        //向服务器请求数据
        SingFriendReq singFriendReq = new SingFriendReq(pageSize, currentPage, listType.getValue());
        HttpLoader.getInstance().sendRequest(new SingFriendHandler(singFriendReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<SingModel> list = ((SingPageInfoModel) model).getSingList();
                for (int i = 1; i <= list.size(); i++) {
                    list.get(i - 1).setDramaSinger(i + "." + list.get(i - 1).getDramaSinger());
                }
                mAdapter.updateData(list);
            }
        }, 0));
    }

    /**
     * 请求服务器，并获取 榜单-专业 数据
     *
     * @param listType 榜单类型
     * @return
     * @author yaohuix
     * @date 2016/3/10 17:11
     */
    public void sendAndGetProfessListData(int pageSize, int currentPage, EListType listType) {
        //向服务器请求数据
        SingProfessReq singProfessReq = new SingProfessReq(pageSize, currentPage, listType.getValue());
        HttpLoader.getInstance().sendRequest(new SingProfessHandler(singProfessReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<SingModel> list = ((SingPageInfoModel) model).getSingList();
                for (int i = 1; i <= list.size(); i++) {
                    list.get(i - 1).setDramaSinger(i + "." + list.get(i - 1).getDramaSinger());
                }
                mRecyclerView.setAdapter(new TopListAdapter(mContext, list));

                //在唱戏，专业请求数据完成之后延迟1.5s做版本更新检查
                if (type == TYPE_PROFESSIONAL) {
                    mRecyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            new AppUpdateEngineImpl().checkAppUpdate();
                        }
                    }, 1500);

                }
            }
        }, 0));
    }

    /**
     * 当点击 总榜 月榜 周榜的时候 更新数据
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/10 17:43
     */
    public void updateData(EListType eListType) {
        if (type == TYPE_PROFESSIONAL) {
            sendAndGetProfessListData(15, 1, eListType);
        } else {
            sendAndGetDramaListData(15, 1, eListType);
        }
    }

}
